//
//  ESCardHoroscopeDAO.m
//  EasyStyle
//
//  Created by FK on 16/1/20.
//  Copyright © 2016年 FK. All rights reserved.
//

#import "ESMainCardDAO.h"

@implementation ESMainCardDAO

+ (ESMainCardDAO *)shareInstance
{
    static ESMainCardDAO *instance = nil;
    
    static dispatch_once_t once;
    
    dispatch_once(&once, ^{
        instance = [[self alloc]init];
        [instance createEditableCopyOfDatabaseIfNeeded];
    });
    return instance;
}

- (id)init
{
    self = [super init];
    if (self) {
        dbPath = [ESDBBaseDAO applicationDocumentsDirectoryFile:CardPlist];
    }
    return self;
}

- (void)createEditableCopyOfDatabaseIfNeeded{
    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL plistExits = [fileManager fileExistsAtPath:dbPath];
    if (!plistExits) {
        NSDictionary *dict = [NSDictionary dictionary];
        [dict writeToFile:dbPath atomically:YES];
    }
}


#pragma mark -- 星座运势
- (BOOL)insertHoroscopeData:(NSDictionary *)horoscopeDict
{
    NSMutableDictionary *allDict = [[NSMutableDictionary alloc] initWithContentsOfFile:dbPath];
    allDict[ESCardDAOKey_Horoscope] = horoscopeDict;
    return [allDict writeToFile:dbPath atomically:YES];
    
}
- (NSDictionary *)queryHoroscopeDataWithType:(NSString *)typeStr
{
    NSMutableDictionary *allDict = [[NSMutableDictionary alloc] initWithContentsOfFile:dbPath];
    NSDictionary *curDict = allDict[ESCardDAOKey_Horoscope][typeStr];
    return curDict;
}


#pragma mark -- 天气状况
- (BOOL)insertWeatherData:(NSDictionary *)horoscopeDict
{
    NSMutableDictionary *allDict = [[NSMutableDictionary alloc] initWithContentsOfFile:dbPath];
    allDict[ESCardDAOKey_Weather] = horoscopeDict;
    return [allDict writeToFile:dbPath atomically:YES];
    
}
- (NSDictionary *)queryWeatherData
{
    NSMutableDictionary *allDict = [[NSMutableDictionary alloc] initWithContentsOfFile:dbPath];
    NSDictionary *curDict = allDict[ESCardDAOKey_Weather];
    return curDict;
}


#pragma mark -- 新闻热词
- (BOOL)insertNewsWordsData:(NSArray *)wordsList
{
    NSMutableDictionary *allDict = [[NSMutableDictionary alloc] initWithContentsOfFile:dbPath];
    allDict[ESCardDAOKey_NewsWords] = wordsList;
    return [allDict writeToFile:dbPath atomically:YES];
    
}
- (NSMutableArray *)queryNewsWordsData
{
    NSMutableDictionary *allDict = [[NSMutableDictionary alloc] initWithContentsOfFile:dbPath];
    NSMutableArray *reData = allDict[ESCardDAOKey_NewsWords];
    return reData;

}


#pragma mark -- 最新笑话
- (BOOL)insertLastedJokesData:(NSArray *)wordsList
{
    NSMutableDictionary *allDict = [[NSMutableDictionary alloc] initWithContentsOfFile:dbPath];
    allDict[ESCardDAOKey_LastedJokes] = wordsList;
    return [allDict writeToFile:dbPath atomically:YES];
}
- (NSArray *)queryLastedJokesData
{
    NSMutableDictionary *allDict = [[NSMutableDictionary alloc] initWithContentsOfFile:dbPath];
    NSArray *reData = allDict[ESCardDAOKey_LastedJokes];
    return reData;
}

@end
